package com.xuzhijie.pms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuzhijie.pms.common.Constants;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Role;
import com.xuzhijie.pms.exception.PmsServerException;
import com.xuzhijie.pms.mapper.RoleMapper;
import com.xuzhijie.pms.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-12
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public Result findPage(Integer page, Integer size, String name, String authorityString, String status, Date startTime, Date endTime) {
        System.out.println("查询先执行了1：查询开始");
        Page<Role> rolePage = new Page<>(page,size);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        Page<Role> result;
        long total;
        if (StrUtil.isNotBlank(name)){
            queryWrapper.like("role_name",name);
        }
        if (StrUtil.isNotBlank(authorityString)){
            queryWrapper.like("authority_string",authorityString);
        }
        if (StrUtil.isNotBlank(status)){
            queryWrapper.eq("status", NumberUtil.parseInt(status));
        }
        if (startTime != null && endTime != null){
            queryWrapper.between("create_time",startTime,endTime);
        }

        try {
            System.out.println("查询先执行了2：查询准备查询数据库");
            result = page(rolePage, queryWrapper);
            System.out.println("查询先执行了3：查询获得数据库数据");
            total = count(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            throw new PmsServerException("业务繁忙", Constants.SYSTEM_ERROR);
        }
        List<Role> records = result.getRecords();

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("records",records);
        resultMap.put("total",total);
        return Result.success(resultMap);
    }

    @Override
    public Result deleteAdmin(String[] ids) {
        HashSet<String> set = new HashSet<>(Arrays.asList(ids));
        int deleteNumber = roleMapper.deleteBatchIds(set);
        return Result.success(deleteNumber);
    }

    @Override
    public Result editRole(Role role) {
        System.out.println("更行先执行了1：更新开始");
        if (BeanUtil.isEmpty(role)){
            return Result.error(Constants.ILLEGAL_PARAMETERS,"参数错误");
        }
        Role newRole = getById(role.getRoleId());
        if (BeanUtil.isEmpty(newRole)){
            return Result.error(Constants.ILLEGAL_PARAMETERS,"角色不存在");
        }
        BeanUtils.copyProperties(role,newRole);
//        //Wrappers.update(newRole).eq("roleId",role.getRoleId());
//        Wrappers.lambdaUpdate().eq(role::getRoleId,newRole)
        System.out.println("更行先执行了2：更新准备执行");
//        lambdaUpdate().eq(Role::getRoleId,role.getRoleId()).update(newRole);
//        update(newRole, new UpdateWrapper<Role>().eq("role_Id",role.getRoleId()));
        roleMapper.update(newRole, new UpdateWrapper<Role>().eq("role_Id",role.getRoleId()));
        System.out.println("更行先执行了3：更新执行完毕");
        return Result.success(newRole);
    }

    @Override
    public Result createRole(String roleName, String authorityString, String status, String funcMenu) {
        if (StrUtil.isAllBlank(roleName,authorityString,status,funcMenu)) {
            Result.error(Constants.ILLEGAL_PARAMETERS,"参数错误");
        }
        Role role = new Role();
        role.setRoleName(roleName);
        role.setAuthorityString(authorityString);
        role.setStatus(Integer.parseInt(status));
        role.setFuncMenu(funcMenu);
        int insert = roleMapper.insert(role);

        return Result.success(insert==1?"新建角色完成":"失败");
    }
}
